package com.glasses.spectrum;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.model.menu.DefaultMenuItem;
import org.primefaces.model.menu.DefaultMenuModel;
import org.primefaces.model.menu.MenuModel;

/**
 *
 * @author Jean-Luc Burot
 * @since 2014-12-14
 */
@Named
@RequestScoped
public class LanguageBean {
    private MenuModel model;
    private List<LanguageItem> languages;
    @Inject
    private GetParam getParams;
    
    public MenuModel getModel() {
        if (this.model == null) {
            initMenu();
        }
        
        return model;
    }
    
    public void setModel(MenuModel model) {
        this.model = model;
    }
    
    public GetParam getGetParam() {
        return this.getParams;
    }
    
    public void setGetParam(GetParam getParam) {
        this.getParams = getParam;
    }
    
    public String getDisplayLanguage() {
        String displayLanguage = "";
        
        //Get appropriate language.
        for(LanguageItem languageItem : this.languages) {
            if (languageItem.getAlias().equals(this.getParams.getLanguage())) {
                displayLanguage = languageItem.getFullNameEn();
            }
        }
        
        return displayLanguage;
    }
    
    public LanguageBean() {
        this.languages = new ArrayList<>();
        
        //Define Language Menu
        this.languages.add(new LanguageItem("en", "English"));
        this.languages.add(new LanguageItem("fr", "Français"));
        this.languages.add(new LanguageItem("de", "German"));
        this.languages.add(new LanguageItem("jp", "日本語"));
    }
    
    private void initMenu() {
        this.model = new DefaultMenuModel();
        
        //Build Language Menu
        for (LanguageItem languageItem : this.languages) {
            DefaultMenuItem menuItem = new DefaultMenuItem();
            
            //Set targeted facelet
            menuItem.setOutcome("index");
            
            //Set language name
            menuItem.setValue(languageItem.getFullNameEn());
            
            //Set language param
            menuItem.setParam("language", languageItem.getAlias());
            
            //Set view if existent
            if(!"".equals(this.getParams.getView())) {
                menuItem.setParam("view", this.getParams.getView());
            }
            
            this.model.addElement(menuItem);
        }
    }
}
